import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutterjc/plugin/sqlite/constant/SQLiteConfigConstant.dart';
import 'package:flutterjc/plugin/sqlite/method/SQLiteDBMethod.dart';
import 'package:flutterjc/plugin/zpubhttp/http/HttpProtocol.dart';
import 'package:path_provider/path_provider.dart';

void main() {
  runApp(new HttpPlugin());
}

/*
 * 网络请求 <br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
class HttpPlugin extends StatefulWidget {
  State<StatefulWidget> createState() {
    return new HttpPluginState();
  }
}

/*
 * 页面功能 <br/>
 */
class HttpPluginState extends State<StatefulWidget> {
  void initState() {
    super.initState();
    initDB();
  }

  /*
   * 初始化数据库
   */
  void initDB() {
    SQLiteConfigConstant.DATABASE_SQL_PATH.add("assets/sql/create_table.sql");
    SQLiteConfigConstant.DATABASE_SQL_PATH.add("assets/sql/zbcx.sql");
    SQLiteDBMethod.getInstance();
  }

  Widget build(BuildContext context) {
    AppBar appBar = AppBar(
      leading: new IconButton(
        icon: new Icon(Icons.arrow_back),
        onPressed: () {},
      ),
      title: new Text('FlutterDesign'),
      centerTitle: true,
      actions: <Widget>[
        new Container(width: 0, height: 0),
        new Container(width: 0, height: 0),
      ],
      backgroundColor: Colors.blue,
    );

    return MaterialApp(
      home: new Scaffold(
        appBar: appBar,
        body: new Builder(builder: (BuildContext context) {
          return Column(
            children: <Widget>[
              RaisedButton(
                child: Text('查询'),
                onPressed: () {
                  post();
                },
              ),

              RaisedButton(
                child: Text('下载'),
                onPressed: () {
                  download();
                },
              )
            ],
          );
        }),
        backgroundColor: Colors.white,
        bottomNavigationBar: null,
      ),
    );
  }

  post() {
    HttpProtocol protocol = new HttpProtocol();
    protocol.setUrl('http://122.112.155.221:8081/testoa2');
    protocol
        .setService('queryControoler')
        .setMethod('LoginSvr')
        .addParam('cmd', 'login')
        .addParam('username', 'admin')
        .addParam('password', '12345678')
        .addParam('type', '1')
        .addParam('sys', '11')
        .post()
        .then((response) {
      print(response.data);
    }).catchError((e) {
      print(e);
    });
  }

  download() async{
    String path = (await getExternalStorageDirectory()).path+'/test.png';
    print(path);

    HttpProtocol protocol = new HttpProtocol();
    protocol.setUrl('https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3293144012,2335984271&fm=26&gp=0.jpg');
    protocol
        .addParam('cmd', 'login')
        .addParam('username', 'admin')
        .addParam('password', '12345678')
        .addParam('type', '1')
        .addParam('sys', '11')
        .requestDownload(path)
        .then((response) {
      print(response.data);
    }).catchError((e) {
      print(e);
    });
  }

}
